package com.tuanbiao.presenter.setting;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.hzjq.entity.DeviceInfoEntity;
import com.hzjq.lib.BlastKool;
import com.tuanbiao.AppConfig;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.SettingMacController;
import com.tuanbiao.data.callback.OnBaseResultCallback;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/14 10:10
 */
public class SettingMacPresenter extends BasePresenter<SettingMacController.Model, SettingMacController.View> implements SettingMacController.Presenter {
    private DeviceInfoEntity mDeviceInfoEntity;

    public SettingMacPresenter(SettingMacController.Model model, SettingMacController.View rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
//        mModel.getSettingParams().getMac();
//        BlastKool.Companion.loader().quick().getSettings();
        mRootView.showMacAndIMEI( mModel.getSettingParams().getMac(),  mModel.getSettingParams().getImei());
//        mModel.getDeviceInfo(entity -> {
//            mDeviceInfoEntity = entity;
//            Timber.i("init:" + entity);
//            mRootView.showMacAndIMEI(entity.getMac(), entity.getIem());
//            if (AppConfig.showTestData()) {
//                mRootView.showMacAndIMEI("b4:29:3d:5a:5c:1f", "863981042855723");
//            }
//        });

    }

//    @Override
//    public void setMac(String mac) {
//        if (TextUtils.isEmpty(mac)) {
//            mRootView.showMessage("请输入MAC地址");
//            return;
//        }
//        if (null != mDeviceInfoEntity) {
//            mDeviceInfoEntity.setMac(mac);
//            mModel.updateDeviceInfo(mDeviceInfoEntity, unused -> {
//                mModel.uploadDeviceInfo(aBoolean -> {
//
//                });
//                mRootView.showMac(mac);
//                mRootView.showMessage("修改成功！");
//            });
//        }
//    }
//
//    @Override
//    public void setIMEI(String IMEI) {
//        if (TextUtils.isEmpty(IMEI)) {
//            mRootView.showMessage("请输入IMEI地址");
//            return;
//        }
//        if (null != mDeviceInfoEntity) {
//            mDeviceInfoEntity.setIem(IMEI);
//            mModel.updateDeviceInfo(mDeviceInfoEntity, unused -> {
//                mModel.uploadDeviceInfo(aBoolean -> {
//
//                });
//                mRootView.showIMEI(IMEI);
//                mRootView.showMessage("修改成功！");
//            });
//        }
//    }

    @Override
    public void setMacAndIMEI(String mac, String IMEI) {
        if (TextUtils.isEmpty(mac)) {
            mRootView.showMessage("请输入MAC地址");
            return;
        }
        if (TextUtils.isEmpty(IMEI)) {
            mRootView.showMessage("请输入IMEI地址");
            return;
        }
        DeviceInfoEntity entity = new DeviceInfoEntity();
        entity.setIem(IMEI);
        entity.setMac(mac);
        Timber.i("setMacAndIMEI#mac:" + mac + ",IMEI:" + IMEI);
        mModel.updateDeviceInfo(entity, unused -> {
            mRootView.showMacAndIMEI(mac, IMEI);
            mRootView.showMessage("修改成功！");

            mModel.uploadDeviceInfo(new OnBaseResultCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean aBoolean) {

                }

                @Override
                public void onFailed(String msg) {

                    mRootView.showMessage(msg);
                }
            });
        });
    }

    @Override
    public String getOriginalMac() {
        if (null != mDeviceInfoEntity) return mDeviceInfoEntity.getOriginalMac();
        return mModel.getSettingParams().getDeviceMac();
    }

    @Override
    public String getOriginalIMEI() {
        if (null != mDeviceInfoEntity) return mDeviceInfoEntity.getOriginalImei();
        return mModel.getSettingParams().getDeviceImei();
    }

}
